//
//  TabTableCell.m
//  LBNestScrollTableView
//
//  Created by liubo on 2022/10/15.
//

#import "TabTableCell.h"
#import <Masonry/Masonry.h>
#import "LBNestScrollTableView.h"
#import "TabTitleCell.h"
#import "ContentCell.h"

@interface TabTableCell () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)LBNestScrollTableView  *tableView;

@property (nonatomic, assign)NSInteger currentIndex;

@end

@implementation TabTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpUI];
        [self setUpContraints];
    }
    return self;
}

- (void)setUpUI
{
    [self.contentView addSubview:self.collectionView];
    [self.contentView addSubview:self.tableView];
}

- (void)setUpContraints
{
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.top.mas_equalTo(40);
    }];
}

#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TabTitleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([TabTitleCell  class]) forIndexPath:indexPath];
    cell.indexPath = indexPath;
    UIColor *backColor = indexPath.item == self.currentIndex ? [UIColor cyanColor] : [UIColor whiteColor];
    cell.contentView.backgroundColor = backColor;
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ContentCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ContentCell class])];
    cell.indexPath =  indexPath;
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.tableView) {
        NSArray *cellsArray = self.tableView.visibleCells;
        UITableViewCell *firstcell = cellsArray.firstObject;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:firstcell];
        NSIndexPath *toIndexpath = [NSIndexPath indexPathForItem:indexPath.section inSection:0];
        self.currentIndex = indexPath.section;
        [self.collectionView reloadData];
        [self.collectionView scrollToItemAtIndexPath:toIndexpath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
}

#pragma mark - lazy load

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(80, 40);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        [_collectionView registerClass:[TabTitleCell class] forCellWithReuseIdentifier:NSStringFromClass([TabTitleCell class])];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

- (LBNestScrollTableView *)tableView
{
    if (!_tableView) {
        _tableView = [[LBNestScrollTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [_tableView registerClass:[ContentCell class] forCellReuseIdentifier:NSStringFromClass([ContentCell class])];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.type = SpecialTableViewTypeSub;
    }
    return _tableView;
}

@end
